package com.csun.cmny.provider.controller;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.dao.OmcNursingRoomBindMapper;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.constant.ReviseConstant;
import com.csun.cmny.provider.model.domain.OmcNursingUserBind;
import com.csun.cmny.provider.model.dto.check_in.CheckInDaysDto;
import com.csun.cmny.provider.model.dto.revise.*;
import com.csun.cmny.provider.model.vo.check_in.CheckInDaysVo;
import com.csun.cmny.provider.model.vo.revise.*;
import com.csun.cmny.provider.pojo.NursingGroup;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.provider.vo.MyCommentVo;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-09-09 16:55
 * @Description: the class omc revise controller
 */
@Slf4j
@RestController
@RequestMapping(value = "/revise", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcReviseController extends BaseController {

    @Resource
    private OmcReviseService omcReviseService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OmcGroupService omcGroupService;
    @Resource
    private OmcNursingRoomBindMapper omcNursingRoomBindMapper;
    @Resource
    private OmcReturnFeeConfigService omcReturnFeeConfigService;
    @Resource
    private OmcNursingUserBindService omcNursingUserBindService;
    @Resource
    private OmcReviseItemService omcReviseItemService;

    /**
     * 根据调整日期查询补缴天数
     */
    @PostMapping(value = "/queryReviseDays")
    public Wrapper queryReviseDays(@RequestBody ReviseDaysDto reviseDaysDto) {

        reviseDaysDto.setGroupId(getLoginAuthDto().getGroupId());
        ReviseDaysVo reviseDaysVo = omcReturnFeeConfigService.queryReviseDaysVo(reviseDaysDto);

        //return WrapMapper.ok(reviseDaysVo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),reviseDaysVo);
    }

    /**
     * 根据调整日期查询补缴天数
     */
    @PostMapping(value = "/queryCheckInDays")
    public Wrapper queryCheckInDays(@RequestBody CheckInDaysDto checkInDaysDto) {

        CheckInDaysVo checkInDaysVo = omcReturnFeeConfigService.queryCheckInDaysVo(checkInDaysDto);

        //return WrapMapper.ok(checkInDaysVo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),checkInDaysVo);
    }

    /**
     * 查询班组可调房调级老人信息
     */
    @PostMapping(value = "/queryOldmanList")
    public Wrapper queryOldmanList() {

        ReviseOldmanQueryDto reviseOldmanQueryDto = new ReviseOldmanQueryDto();

        /**
         * @author  chisj
         * @date    2021-08-17 15:07
         * @desc    修复班长在多个班组无法查询调房调级老人
         */
        // 获取用户是否为班长
        NursingGroup nursingGroup = new NursingGroup();
        nursingGroup.setUserId(getLoginAuthDto().getUserId());
        List<NursingGroup> nursingGroupList = omcGroupService.select(nursingGroup);
        if (nursingGroupList.size() == 0) {
            throw new OmcBizException(ErrorCodeEnum.OMC10038001);
        }
        List<Long> nursingGroupIdList = Lists.newArrayList();
        for (NursingGroup ng : nursingGroupList) {
            nursingGroupIdList.add(ng.getId());
        }
        // reviseOldmanQueryDto.setNursingGroupId(nursingGroup.getId());
        reviseOldmanQueryDto.setNursingGroupIdList(nursingGroupIdList);
        reviseOldmanQueryDto.setGroupId(getLoginAuthDto().getGroupId());

        // 根据班组查询看护的房间
//        List<Integer> roomIdList = omcNursingRoomBindMapper
//                .queryRoomIdListByNursingGroupId(reviseOldmanQueryDto.getNursingGroupId());
        List<Integer> roomIdList = omcNursingRoomBindMapper
                .queryRoomIdListByNursingGroupIdList(nursingGroupIdList);
        if (PublicUtil.isEmpty(roomIdList)) {
            throw new OmcBizException(ErrorCodeEnum.OMC10038011);
        }
        reviseOldmanQueryDto.setRoomIdList(roomIdList);

        List<ReviseOldmanVo> reviseOldmanVoList = omcReviseService.queryOldmanList(reviseOldmanQueryDto);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),reviseOldmanVoList);
    }

    /**
     * 根据老人Id查询调房调级情况
     */
    @PostMapping(value = "/queryByOldmanId/{oldmanId}")
    public Wrapper queryByOldmanId(@PathVariable Integer oldmanId, String reviseDate) {

        OldmanFeeVo oldmanFeeVo = omcReviseService.queryByOldmanId(oldmanId, getLoginAuthDto(), reviseDate);

        //return WrapMapper.ok(oldmanFeeVo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),oldmanFeeVo);
    }

    /**
     * 查询调房调级列表 - 分页查询
     */
    @PostMapping(value = "/queryListWithPage")
    public Wrapper queryListWithPage(@RequestBody ReviseQueryDto reviseQueryDto) {

        /**
         * @author  chisj
         * @date    2021-08-17 14:55
         * @desc    修复班长在多个班组无法查询调房调级
         */
        /**
         * @date    2019-09-26
         * @author  chisj
         * @desc    按班组查询
         */
        OmcNursingUserBind nursingUserBind = new OmcNursingUserBind();
        nursingUserBind.setUserId(getLoginAuthDto().getUserId());
        List<OmcNursingUserBind> nursingUserBindList = omcNursingUserBindService.select(nursingUserBind);
        if (PublicUtil.isNotEmpty(nursingUserBindList)) {
            List<Long> nursingGroupIdList = Lists.newArrayList();
            for (OmcNursingUserBind onub : nursingUserBindList) {
                nursingGroupIdList.add(onub.getNursingGroupId());
            }
            reviseQueryDto.setNursingGroupIdList(nursingGroupIdList);
        }
//        if (nursingUserBind != null && nursingUserBind.getNursingGroupId() != null) {
//            reviseQueryDto.setNursingGroupId(nursingUserBind.getNursingGroupId());
//        }
        reviseQueryDto.setGroupId(getLoginAuthDto().getGroupId());

        List<ReviseVo> reviseVoList = omcReviseService.queryListWithPage(reviseQueryDto);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),new PageInfo<>(reviseVoList));
    }

    /**
     * 根据Id调房调级详情
     */
    @PostMapping(value = "/queryById/{id}")
    public Wrapper queryById(@PathVariable Long id) {

        ReviseVo reviseVo = omcReviseService.queryById(id);

        //return WrapMapper.ok(reviseVo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),reviseVo);
    }

    /**
     * 查询待审批的列表 - 分页查询
     */
    @PostMapping(value = "/queryCurTask")
    public Wrapper queryCurTask(@RequestBody ReviseQueryDto reviseQueryDto) {

        List<Integer> states = omcReviseService.queryCurTaskStates(getLoginAuthDto());

        if (PublicUtil.isEmpty(states) || states.size() < 1) {
            return WrapMapper.ok(new PageInfo<>(Lists.newArrayList()));
        }

        // 对states进行去重
        PublicUtil.removeDuplicate(states);

        reviseQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        reviseQueryDto.setStates(states);

        List<ReviseVo> reviseVoList = omcReviseService.queryListWithPage(reviseQueryDto);

        //return WrapMapper.ok(new PageInfo<>(reviseVoList));
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), new PageInfo<>(reviseVoList));
    }

    /**
     * 查询历史列表 - 分页查询
     */
    @PostMapping(value = "/queryHisTask")
    public Wrapper queryHisTask(@RequestBody ReviseQueryDto reviseQueryDto) {

        List<Integer> states = Lists.newArrayList();
        states.add(ReviseConstant.REVISE_STATE_SERVICE_REFUSE);
        states.add(ReviseConstant.REVISE_STATE_CHARGE_CONFIRM);

        reviseQueryDto.setStates(states);
        reviseQueryDto.setGroupId(getLoginAuthDto().getGroupId());

        List<ReviseVo> reviseVoList = omcReviseService.queryListWithPage(reviseQueryDto);

        //return WrapMapper.ok(new PageInfo<>(reviseVoList));
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),new PageInfo<>(reviseVoList));
    }

    /**
     * 根据Id查询批注
     */
    @PostMapping(value = "/queryCommentById/{id}")
    public Wrapper queryCommentById(@PathVariable Long id) {

        List<MyCommentVo> myCommentVoList = omcReviseService.queryCommentById(id);

        //return WrapMapper.ok(myCommentVoList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),myCommentVoList);
    }

    /**
     * 申请调房调级
     */
    @PostMapping(value = "/apply")
    public Wrapper apply(@RequestBody ReviseDto reviseDto) {

        Integer result = omcReviseService.apply(reviseDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

    /**
     * 服务部长审批
     */
    @PostMapping(value = "/serviceApproval")
    public Wrapper serviceApproval(@RequestBody ReviseApprovalDto reviseApprovalDto) {

        Integer result = omcReviseService.serviceApproval(reviseApprovalDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

    /**
     * 接待部确认 (评估员也可以)
     */
    @PostMapping(value = "/admitConfirm")
    public Wrapper admitConfirm(@RequestBody ReviseConfirmDto reviseConfirmDto) {

        Integer result = omcReviseService.admitConfirm(reviseConfirmDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

    /**
     * 缴费确认
     */
    @PostMapping(value = "/chargeConfirm")
    public Wrapper chargeConfirm(@RequestBody ReviseConfirmDto reviseConfirmDto) {

        Integer result = omcReviseService.chargeConfirm(reviseConfirmDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

    /**
     * 测试接口 -- 查询调房调级收费项列表
     */
    @PostMapping(value = "/queryReviseItemListWithPage")
    public Wrapper queryReviseItemListWithPage(@RequestBody ReviseItemQueryDto reviseItemQueryDto) {

        List<ReviseItemVo> reviseItemVoList = omcReviseItemService.queryListWithPage(reviseItemQueryDto);

        //return WrapMapper.ok(reviseItemVoList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),reviseItemVoList);
    }
}
